package com.mytest.leetcode;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

@Slf4j
public class Solution977 {

    public static void main(String[] args) {
        Solution977 solution977 = new Solution977();
//        int [] nums = {-4,-1,0,3,10};
        int [] nums = {-7,-3,2,3,11};
        nums = new int[]{-7, -3, 2, 3, 11};
        int [] result = solution977.sortedSquares(nums);
        log.info("{}", result);
    }
    public int[] sortedSquares(int[] nums) {
        int [] result = new int[nums.length];
        int left = 0;
        int right = nums.length -1;
        int i = nums.length -1;

        while(left<=right){
            if(nums[left]*nums[left] > nums[right]*nums[right]){
                result[i] = nums[left]*nums[left];
                left++;
            }else{
                result[i] = nums[right]*nums[right];
                right--;
            }
            i--;
        }
        return result;
    }


    public int[] sortedSquares1(int[] nums) {
        int [] result = new int[nums.length];
        int left = 0;
        int right = nums.length -1;
        int i = 0;
        while(left<=right){
            result[i] = nums[left]*nums[left];
            left++;
            i++;
            if (left < right) {
                result[i] = nums[right]*nums[right];
                right--;
                i++;
            }
        }
        Arrays.sort(result);
        return result;
    }
}
